package com.example.audiobooks.utils;

import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtils {



//    static {
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//
//    }

    public static Connection getConn() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        final String TAG = "mysql-party-JDBCUtils";
//        String driver = "com.mysql.jdbc.Driver";// MySql驱动
        String url = "jdbc:mysql://192.168.0.113:3306/test";// 数据库名称
        String user = "root";// 用户名
        String password = "123456";// 密码

        Connection  conn = null;
        try {

            conn = DriverManager.getConnection(url,user,password);
            Log.d("TAG", "getConn: 数据库连接成功"+conn);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn){
        if (conn == null){
            Log.d("JDBCUtils", "未连接上数据库");
        }else{
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

}


